package com.example.service.impl;

import com.example.mapper.SeckillMapper;
import com.example.model.Seckill;
import com.example.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @Author: 胡成
 * @Version: 0.0.1V
 * @Date: 2019/2/2
 * @Description: 类描述
 **/
@Service
public class SeckillServiceImpl implements SeckillService {

    @Autowired
    private SeckillMapper seckillMapper;

    //@Cacheable(value = "db", key = "T(com.ghaoqi.xcx.core.constant.RedisConstant).XCX_CATEGORY_PAGE_KEY + #tableName")
    @Cacheable(value = "seckill", key = "'seckill:item:' + #seckillId")
    @Override
    public Seckill get(long seckillId) {
        System.out.println("执行这里，说明缓存中读取不到数据，直接读取数据库....");
        Seckill seckill = seckillMapper.selectByPrimaryKey(seckillId);
        return seckill;
    }

    @CachePut(value = "seckill",key = "'seckill:item:' + #seckill.getSeckillId()")
    @Override
    public Seckill updateByPrimaryKeySelective(Seckill seckill) {
        System.out.println("执行这里，更新数据库，更新缓存....");
        seckillMapper.updateByPrimaryKeySelective(seckill);
        return seckillMapper.selectByPrimaryKey(seckill.getSeckillId());
    }

    @CacheEvict(value = "seckill",key = "'seckill:item:'+#seckillId")
    @Override
    public String delete(long seckillId) {
        seckillMapper.deleteByPrimaryKey(seckillId);
        return "ok";
    }

}
